package com.zzz.excelanalyse

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import kotlin.concurrent.thread

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //将assets中的文件存为手机文件
        val xlsFile = File(filesDir, "ExcelTest.xls")
        thread {
            val xlsIs = assets.open("ExcelTest.xls")
            xlsIs.use { inputStream ->
                xlsFile.writeBytes(inputStream.readBytes())
            }
        }
        val xlsxFile = File(filesDir, "ExcelTest.xlsx")
        thread {
            val xlsxIs = assets.open("ExcelTest.xlsx")
            xlsxIs.use { inputStream ->
                xlsxFile.writeBytes(inputStream.readBytes())
            }
        }
        //这里就不判断上面的操作是否完成
        btnXlsJxl.setOnClickListener {
            thread {
                val start = System.currentTimeMillis()
                showExcel(ExcelManager.INSTANCE.xlsJxl(xlsFile))
                Log.i("lulu", "time -> ${System.currentTimeMillis() - start}")
            }
        }
        btnXlsxSelf.setOnClickListener {
            thread {
                val start = System.currentTimeMillis()
                showExcel(ExcelManager.INSTANCE.xlsxSelf(xlsxFile))
                Log.i("lulu", "time -> ${System.currentTimeMillis() - start}")
            }
        }
        btnXlsPOI.setOnClickListener {
            thread {
                val start = System.currentTimeMillis()
                showExcel(ExcelManager.INSTANCE.excelPOI(xlsFile))
                Log.i("lulu", "time -> ${System.currentTimeMillis() - start}")
            }
        }
        btnXlsxPOI.setOnClickListener {
            thread {
                val start = System.currentTimeMillis()
                showExcel(ExcelManager.INSTANCE.excelPOI(xlsxFile))
                Log.i("lulu", "time -> ${System.currentTimeMillis() - start}")
            }
        }
    }

    private fun showExcel(data: Map<String, List<List<String>>>) {
        data.forEach {
            Log.i("lulu", "sheet-> ${it.key}")
            it.value.forEach { rowsDataList ->
                Log.i("lulu", rowsDataList.toString())
            }
        }
    }

}
